﻿using WebSelfHost.Logic.Sys;
using WebSelfHost.Entity.Sys;
using WebSelfHost.Utility.ResponseModels;
using WebSelfHost.Utility.Core; 
using WebSelfHost.Logic;
using WebSelfHost.Utility.Operator;
using Microsoft.AspNetCore.Mvc;
using WebSelfHost.Entity.WebRequestParms.Sys.ItemDetail;
using WebSelfHost.Entity.WebRequestParms; 
using WebSelfHost.Web.Filters;

#if NETFRAMEWORK
using System.Web.Http;
using OwinSessionMiddleware.WebApi;
#else
using Microsoft.AspNetCore.Http; 
#endif

namespace WebSelfHost.Web.Controllers.Sys
{
    [HiddenApi]
    public class ItemsDetailController : BaseController
    {
        private SysItemsDetailLogic itemDetaillogic;

        public ItemsDetailController()
        {
            itemDetaillogic = new SysItemsDetailLogic();
        }

        /// <summary>
        /// 数据字典主界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/itemsDetail/index"), AuthorizeChecked]
        public ActionResult Index()
        {
            return Html("/Sys/ItemsDetail/Index");
        }

        /// <summary>
        /// 数据字典主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("system/itemsDetail/index"), AuthorizeChecked]
        public ActionResult Index(ItemDetailIndexParms parms)
        {
            int totalCount = 0;
            var pageData = itemDetaillogic.GetList(parms.pageIndex, parms.pageSize, parms.itemId, parms.keyWord, ref totalCount);
            var result = new LayPadding<SysItemDetail>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改数据字典界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/itemsDetail/form"), AuthorizeChecked]
        public ActionResult Form()
        {
            return Html("/Sys/ItemsDetail/Form");
        }

        /// <summary>
        /// 新增/修改数据字典数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("system/itemsDetail/form"), AuthorizeChecked]
        public ActionResult Form(SysItemDetail model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = itemDetaillogic.Insert(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = itemDetaillogic.Update(model, OperatorProvider.Instance.GetCurrent(Request).Account);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 数据字典详细信息界面
        /// </summary>
        /// <returns></returns>
        [HttpGet, Route("system/itemsDetail/detail"), AuthorizeChecked]
        public ActionResult Detail()
        {
            return Html("/Sys/ItemsDetail/Detail");
        }


        /// <summary>
        /// 数据字典主界面删除按钮方法
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost, Route("system/itemsDetail/delete"), AuthorizeChecked]
        public ActionResult Delete(PrimaryKeyParms parm)
        {
            int row = itemDetaillogic.Delete(parm.primaryKey);
            return row > 0 ? Success() : Error();
        }

        /// <summary>
        /// 根据主键获取数据字典
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost, Route("system/itemsDetail/getForm"), LoginChecked]
        public ActionResult GetForm(PrimaryKeyParms parm)
        {
            SysItemDetail entity = itemDetaillogic.Get(parm.primaryKey);
            entity.IsDefault = entity.IsDefault == "1" ? "true" : "false";
            entity.IsEnabled = entity.IsEnabled == "1" ? "true" : "false";
            return Content(entity.ToJson());
        }

        /// <summary>
        /// 数据字典主界面数据
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/itemsDetail/index")]
        public ActionResult AppIndex(ItemDetailIndexParms parms)
        {
            int totalCount = 0;
            var pageData = itemDetaillogic.GetList(parms.pageIndex, parms.pageSize, parms.itemId, "", ref totalCount);
            var result = new LayPadding<SysItemDetail>()
            {
                result = true,
                msg = "success",
                list = pageData,
                count = totalCount//pageData.Count
            };
            return Content(result.ToJson());
        }

        /// <summary>
        /// 新增/修改数据字典数据提交
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/itemsDetail/form")]
        public ActionResult AppForm(SysItemDetail model)
        {
            if (model.Id.IsNullOrEmpty())
            {
                int row = itemDetaillogic.AppInsert(model, model.CreateUser);
                return row > 0 ? Success() : Error();
            }
            else
            {
                int row = itemDetaillogic.AppUpdate(model, model.ModifyUser);
                return row > 0 ? Success() : Error();
            }
        }

        /// <summary>
        /// 删除数据字典
        /// </summary>
        /// <param name="parms"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/itemsDetail/delete")]
        public ActionResult AppDelete(ItemDeleteParms parms)
        {
            int row = itemDetaillogic.Delete(parms.primaryKey);
            Logger.OperateInfo($"用户{parms.operateUser}删除了字典选项");
            return row > 0 ? Success() : Error();
        }

        /// <summary>
        /// 根据主键获取数据字典
        /// </summary>
        /// <param name="parm"></param>
        /// <returns></returns>
        [HttpPost, Route("app/system/itemsDetail/getForm")]
        public ActionResult AppGetForm(PrimaryKeyParms parm)
        {
            SysItemDetail entity = itemDetaillogic.Get(parm.primaryKey);
            entity.IsDefault = entity.IsDefault == "1" ? "true" : "false";
            entity.IsEnabled = entity.IsEnabled == "1" ? "true" : "false";
            return Content(entity.ToJson());
        }
    }
}
